package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.mongo.Visitor;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的喜欢 粉丝服务实现类
 */
@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 我的喜欢数量userId=xxx 查询likeUserId数量
     *
     * @param userId
     * @return
     */
    @Override
    public Long countByUserId(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    /**
     * 我的喜欢数量likeUserId=xxx 查询userId数量
     *
     * @param userId
     * @return
     */
    @Override
    public Long countByLikeUserId(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    /**
     * 互相喜欢分页查询
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageLikeEachOther(Long userId, int page, int pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.limit(pagesize).skip((page-1)*pagesize);
        long counts = mongoTemplate.count(query, Friend.class);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        //查询RecommendUser
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (Friend friend : friendList) {
            Long currentUserId = friend.getUserId();//当前登录用户id
            Long friendId = friend.getFriendId();//好友id
            recommendUserList.add(getScore(currentUserId, friendId));
        }
        long pages = (counts / pagesize) + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }

    /**
     * 获取缘分值
     * @param currentUserId 当前用户id
     * @param friendId 好友用户id
     * @return
     */
    private RecommendUser getScore(Long currentUserId, Long friendId) {
        Query recommendUserQuery = new Query();
        recommendUserQuery.addCriteria(Criteria.where("userId").is(friendId).and("toUserId").is(currentUserId));
        recommendUserQuery.limit(1);//查询1条记录
        recommendUserQuery.with(Sort.by(Sort.Direction.DESC,"date"));
        RecommendUser recommendUser = mongoTemplate.findOne(recommendUserQuery, RecommendUser.class);
        if(recommendUser == null){
            recommendUser = new RecommendUser();
            recommendUser.setUserId(friendId);//好友id
            recommendUser.setToUserId(currentUserId);//当前用户id
            recommendUser.setScore(88d);//设置默认数据
        }
        return recommendUser;
    }

    /**
     * 我关注分页查询
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageOneSideLike(Long userId, int page, int pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.limit(pagesize).skip((page-1)*pagesize);
        long counts = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        //查询RecommendUser
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            Long currentUserId =  userLike.getUserId();//当前登录用户id
            Long friendId = userLike.getLikeUserId() ;//好友id
            recommendUserList.add(getScore(currentUserId, friendId));
        }
        long pages = (counts / pagesize) + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }

    /**
     * 粉丝分页查询
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageFens(Long userId, int page, int pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(userId));//粉丝查询
        query.limit(pagesize).skip((page-1)*pagesize);
        long counts = mongoTemplate.count(query, UserLike.class);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        //查询RecommendUser
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            Long currentUserId =  userLike.getUserId();//当前登录用户id
            Long friendId = userLike.getLikeUserId() ;//好友id
            recommendUserList.add(getScore(friendId, currentUserId));
        }
        long pages = (counts / pagesize) + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }

    /**
     * 谁看过我分页查询
     *
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<RecommendUser> findPageMyVisitors(Long userId, int page, int pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));//访客查询
        query.limit(pagesize).skip((page-1)*pagesize);
        long counts = mongoTemplate.count(query, Visitor.class);
        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        //查询RecommendUser
        List<RecommendUser> recommendUserList = new ArrayList<>();
        for (Visitor visitor : visitorList) {
            Long currentUserId =  visitor.getUserId();//当前登录用户id
            Long friendId = visitor.getVisitorUserId() ;//好友id
            recommendUserList.add(getScore(currentUserId, friendId));
        }
        long pages = (counts / pagesize) + (counts%pagesize > 0 ? 1:0);
        return new PageResult<>(counts,(long)pagesize,pages,(long)page,recommendUserList);
    }

    /**
     * 根据当前用户id 与 好友id 删除UserLike中 粉丝-喜欢记录
     * @param userId
     * @param friendId
     */
    @Override
    public void deleteById(Long userId, Long friendId) {
        Query query = new Query();
        //userId:2 friendId:6
        query.addCriteria(Criteria.where("likeUserId").is(userId).and("userId").is(friendId));
        mongoTemplate.remove(query,UserLike.class);
    }
}
